<template>
  <div v-if="productList.length > 0" class="product">
    <div class="product-float">
      <div class="product-record-btn" @click="onExchangeRecord">兑换记录</div>
    </div>
    <div class="product-content">
      <div class="product-item" v-for="item in productList" :key="item.id">
        <img
          class="product-item--img"
          :src="`${imgPrefix}${item.thumbnailUrl}`"
        />
        <div class="product-item--content">
          <div class="product-item--title">
            {{ item.name }}
          </div>
          <div class="product-item--subtitle">{{ item.introductionText }}</div>
          <div class="product-item--action" @click="onServiceDetails(item)">
            <div>具体请查看</div>
            <div><strong>服务细则</strong></div>
            <img src="../../assets/product-arrow.png" />
          </div>
        </div>
        <div class="product-item--btn" @click="onExchange(item)">去兑换</div>
      </div>
    </div>
    <van-popup round v-model:show="isExchangePopupVisble">
      <div class="exchange-popup">
        <div class="exchange-popup-title">{{ currentProduct.name }}</div>
        <div class="exchange-popup-text">
          会员权益一经兑换不可退换， 请在有效期内完成使用。
        </div>
        <div class="exchange-popup-action">
          <div class="exchange-popup-btn" @click="onCancelExchange">
            取消兑换
          </div>
          <div class="exchange-popup-btn" @click="onConfirmExchange">
            确认兑换
          </div>
        </div>
      </div>
    </van-popup>
  </div>
  <div v-else-if="!isLoading" class="product-empty">
    <img class="product-empty__img" src="../../assets/product-empty.png" />
    <div class="product-empty__text">去其他地方逛逛吧～</div>
  </div>
  <div v-else-if="isLoading" class="load-page">
    <van-loading type="spinner" />
  </div>
</template>

<script>
import useProduct from './hooks/useProduct'
import useExchangePopup from './hooks/useExchangePopup'
import globalVariables from '@/utils/globalVariables'

export default {
  setup() {
    const { productList, isLoading, onExchangeRecord, onServiceDetails } =
      useProduct()
    const {
      isExchangePopupVisble,
      currentProduct,
      onExchange,
      onConfirmExchange,
      onCancelExchange
    } = useExchangePopup()

    return {
      productList,
      isLoading,
      imgPrefix: globalVariables.imgPrefix,
      currentProduct,
      onExchangeRecord,
      onServiceDetails,
      onExchange,
      isExchangePopupVisble,
      onCancelExchange,
      onConfirmExchange
    }
  }
}
</script>

<style lang="less" scoped="scoped">
.product {
  background-color: #121314;
  background-image: url(../../assets/product-back.png);
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-position: top center;
  min-height: 100vh;

  .product-float {
    position: absolute;
    top: 4px;
    right: 0;
    .product-record-btn {
      width: 64px;
      height: 24px;
      background-image: url(../../assets/product-float-btn.png);
      background-size: 140% 100%;

      font-size: 12px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #333333;

      box-sizing: border-box;
      padding: 4px 0 0 10px;
    }
  }
  .product-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-bottom: 18px;
    .product-item {
      width: 346px;
      height: 80px;
      background-image: url(../../assets/product-item-back.png);
      background-size: 100% 100%;
      margin-top: 16px;

      display: flex;
      .product-item--img {
        width: 60px;
        height: 60px;
        margin-top: 8px;
        margin-left: 11px;
      }
      .product-item--content {
        flex: 1;
        color: #e5ca9d;
        margin-top: 13px;
        margin-left: 30px;
        font-family: Source Han Sans CN;
        .product-item--title {
          font-size: 14px;
          font-weight: 400;
          white-space: nowrap;
        }
        .product-item--subtitle {
          font-size: 11px;
          margin-top: 5px;
        }
        .product-item--action {
          margin-top: 4px;
          font-size: 11px;
          display: flex;
          align-items: center;
          div {
            margin-right: 4px;
          }
          img {
            width: 10px;
            height: 12px;
          }
        }
      }
      .product-item--btn {
        width: 69px;
        height: 25px;
        background-image: url(../../assets/product-btn.png);
        background-size: 100% 100%;

        font-size: 12px;
        font-family: Source Han Sans CN;
        font-weight: 500;
        color: #333333;

        box-sizing: border-box;
        padding-top: 3px;
        text-align: center;

        margin-top: 32px;
        margin-right: 20px;
      }
    }
  }
}
.exchange-popup {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 347px;
  height: 282px;
  background-image: url(../../assets/product-dialog-back.png);
  background-size: 100% 100%;
  .exchange-popup-title {
    font-size: 17px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
    margin-top: 89px;
  }
  .exchange-popup-text {
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #333333;
    margin-top: 26px;
    width: 185px;
  }
  .exchange-popup-action {
    margin-top: 35px;
    display: flex;
    justify-content: space-between;
    width: 255px;
    .exchange-popup-btn {
      width: 110px;
      height: 36px;
      border-radius: 18px;
      background: linear-gradient(-4deg, #ebd9c0, #efb26d);
      box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.67),
        0px 1px 0px 0px rgba(255, 255, 255, 0.96);

      font-size: 15px;
      font-family: Source Han Sans CN;
      font-weight: 500;
      color: #333333;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.product-empty {
  background-color: #121314;
  background-image: url(../../assets/product-back.png);
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-position: top center;
  min-height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .product-empty__img {
    margin-top: -80px;
    width: 300px;
    height: 190px;
  }
  .product-empty__text {
    margin-top: 27px;
    font-size: 14px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #999999;
  }
}
</style>
